﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HYRElectric.LightningCalculation.Forms;

namespace HYRElectric.LightningCalculation
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdLightningCalculation : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			Result result;
			try
			{
				Process currentProcess = Process.GetCurrentProcess();
				if (currentProcess != null)
				{
					WindowHandle owner = new WindowHandle(currentProcess.MainWindowHandle);
					try
					{
						MainForm mainForm = new MainForm(cmdData);
						if (mainForm.ShowDialog(owner) == DialogResult.OK)
						{
							try
							{
								new GridDrawingKit(mainForm.GetCalculationData()).DrawingGrid(cmdData);
							}
							catch (Exception ex)
							{
								MessageBox.Show(ex.Message);
							}
						}
						return Autodesk.Revit.UI.Result.Succeeded;
					}
					catch (Exception ex2)
					{
						MessageBox.Show(ex2.Message);
						return Autodesk.Revit.UI.Result.Failed;
					}
				}
				MessageBox.Show("未能得到当前Revit进程！");
				result = Autodesk.Revit.UI.Result.Failed;
			}
			catch (Exception ex3)
			{
				MessageBox.Show(ex3.Message);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}
	}
}
